package com.lvmama.dashboard.service.ssh.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lvmama.dashboard.core.ssh.RmtShellRunner;
import com.lvmama.dashboard.dao.ssh.IShellExecDao;
import com.lvmama.dashboard.dao.ssh.IShellOutFileDao;
import com.lvmama.dashboard.dao.ssh.IShellTaskDao;
import com.lvmama.dashboard.entity.ShellExec;
import com.lvmama.dashboard.entity.ShellOutFile;
import com.lvmama.dashboard.entity.ShellTask;
import com.lvmama.dashboard.exception.ServiceException;
import com.lvmama.dashboard.service.ssh.IShellExecService;
import com.lvmama.dashboard.utils.ErrDict;
import com.lvmama.java.rhino.spark.utils.Constants;
import com.lvmama.rhino.utils.JedisClusterUtils;

@Service
public class ShellExecServiceImpl implements IShellExecService {
	@Autowired
	private IShellExecDao shellExecDao;
	@Autowired
	private IShellOutFileDao shellOutFileDao;
	@Autowired
	private IShellTaskDao shellTaskDao;
	
	@Override
	@Transactional
	public ShellExec saveShellExecInfo(String execHost, String launchHost, String shellName) {
		ShellOutFile shellOutFile = new ShellOutFile();
		String fileName = shellName + "_" + System.currentTimeMillis() + ".txt";
		fileName = fileName.replace(" ", "").replace("\\", "").replace("/", "");
		shellOutFile.setFileName(fileName);
		int result = shellOutFileDao.insert(shellOutFile);
		if(result != 1) {
			throw new ServiceException(ErrDict.SHELL.SAVE_ERR.getMsg());
		}
		ShellExec shellExec = new ShellExec();
		shellExec.setShellName(shellName);
		shellExec.setShellOutFile(shellOutFile);
		shellExec.setExecHost(execHost);
		shellExec.setLaunchHost(launchHost);
		shellExec.setStatus(Constants.SHELL_STATUS.RUNNING.getCode());
		result = shellExecDao.insert(shellExec);
		if(result != 1) {
			throw new ServiceException(ErrDict.SHELL.SAVE_ERR.getMsg());
		}
		return shellExec;
	}

	@Override
	@Transactional
	public boolean updateShellExecStatus(int execId, Constants.SHELL_STATUS status, String info) {
		ShellExec shellExec = new ShellExec();
		shellExec.setExecId(execId);
		shellExec.setMemo(info);
		shellExec.setStatus(status.getCode());
		shellExec.setEndTime(new Date());
		int result = shellExecDao.update(shellExec);
		if(result != 1) {
			throw new ServiceException(ErrDict.SHELL.UPDATE_ERR.getMsg());
		}
		return true;
	}

	@Override
	public Map<String, List<?>> queryByTaskName(String taskName) {
		ShellTask shellTask = shellTaskDao.queryByTashName(taskName);
		if(shellTask == null) {
			throw new ServiceException(ErrDict.SHELL.NOT_EXIST_JOB_NAME.getMsg());
		}
		List<ShellExec> shellExecList = shellExecDao.queryListByShellName(shellTask.getShellName(), 0, 20);
		List<Integer> execIdList = new ArrayList<Integer>();
		if(CollectionUtils.isNotEmpty(shellExecList)) {
			for(ShellExec shellExec : shellExecList) {
				execIdList.add(shellExec.getExecId());
			}
		}
		Map<String, Object> shellQueue = JedisClusterUtils.getInstance().hmget("shell_queue");
		Collection<Object> values = shellQueue.values();
		Iterator<Object> it = values.iterator();	
		List<String> shellNameList = new ArrayList<String>();
		while (it.hasNext()) {
			Object obj = it.next();
			if(obj instanceof RmtShellRunner) {
				RmtShellRunner runner = (RmtShellRunner) obj;
				shellNameList.add(runner.getShellName());
			}
		}
		Map<String, List<?>> result = new HashMap<String, List<?>>();
		result.put("execIdList", execIdList);
		result.put("waitShellList", shellNameList);
		return result;
	}

	@Override
	public ShellExec queryByExecId(int execId) {
		ShellExec shellExec = shellExecDao.queryById(execId);
		if(shellExec == null) {
			throw new ServiceException(ErrDict.SHELL.NOT_EXIST_EXEC.getMsg());
		}
		return shellExec;
	}

}
